package util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import com.jfinal.plugin.activerecord.Model;

public class AppUtil {
	
	public static SimpleDateFormat time=new SimpleDateFormat("yyyy年MM月dd日");
	
	//是否为整数
	public static boolean isInteger(String str) {
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
        return pattern.matcher(str).matches();
	}
	
	//是否未数字
	public static boolean isNum(String str) {
		Pattern pattern = Pattern.compile("^[-\\+]?[\\d]{1,10}(\\.[\\d]*)?$");
        return pattern.matcher(str).matches();
	}
	
	//是否为电话号码
	public static boolean isphone(String str){
		Pattern pattern = Pattern.compile("^[1][3,4,5,6,7,8][0-9]{9}$");
        return pattern.matcher(str).matches();
	}
	
	//是否为邮箱
	public static boolean isemail(String str){
		Pattern pattern = Pattern.compile("^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$");
        return pattern.matcher(str).matches();
	}
	
	//获取model属性
	public static Map<String, String> getAttr(Model<?> bean){
		Map<String, String> attrMap = new LinkedHashMap<String, String>();
		for (String attr : bean._getAttrNames()) {
			attrMap.put(attr, bean.getStr(attr));
		}
		
		return attrMap;
	}
	
	public static String modelToJson(Model<?> bean){
		StringBuffer sb = new StringBuffer();
		sb.append("{\"code\": 0,\"msg\": \"成功\",\"data\": {");
		String[] attrs = bean._getAttrNames();
		for (String attr : attrs) {
			sb.append("\""+attr+"\":");
			if(bean.getStr(attr)==null){
				sb.append("null");
			}else if("create_time".equals(attr) || "update_time".equals(attr)){
				String value = time.format(new Date(Long.parseLong(bean.getStr(attr))*1000));
				sb.append("\""+value+"\"");
			}else if(AppUtil.isNum(bean.getStr(attr))){
				sb.append(bean.getStr(attr));
			}else if(bean.getStr(attr).startsWith("[")){
				sb.append(bean.getStr(attr));
			}else{
				sb.append("\""+bean.getStr(attr).replace("\"", "\\\"")+"\"");
			}
			if(!attr.equals(attrs[attrs.length-1])){
				sb.append(",");
			}
		}
		sb.append("}}");
		return sb.toString();
	}
	
	public static String modelToJson(List<? extends Model<?>> beanList, int pageCount){
		StringBuffer sb = new StringBuffer();
		sb.append("{\"code\": 0,\"msg\": \"成功\",");
		
		if(pageCount!=0){
			sb.append("\"page\":"+pageCount+",");
		}
		
		sb.append("\"data\": [");
		
		for (Model<?> bean : beanList) {
			sb.append("{");
			String[] attrs = bean._getAttrNames();
			for (String attr : attrs) {
				
				sb.append("\""+attr+"\":");
				if(bean.getStr(attr)==null){
					sb.append("null");
				}else if("create_time".equals(attr) || "update_time".equals(attr)){
					String value = time.format(new Date(Long.parseLong(bean.getStr(attr))*1000));
					sb.append("\""+value+"\"");
				}else if(AppUtil.isNum(bean.getStr(attr))){
					sb.append(bean.getStr(attr));
				}else{
					sb.append("\""+bean.getStr(attr).replace("\"", "\\\"")+"\"");
				}

				if(!attr.equals(attrs[attrs.length-1])){
					sb.append(",");
				}
			}
			sb.append("}");
			
			if(bean!=beanList.get(beanList.size()-1)){
				sb.append(",");
			}
		}
		sb.append("]}");
		return sb.toString();
	}
	
	public static String modelToJson(List<? extends Model<?>> beanList){
		return modelToJson(beanList, 0);
	}
	
	public static String selectSQLGenetate(boolean count, Model<?> bean, String tableName, Integer offset, Integer length){
		
		StringBuffer sql = new StringBuffer();
		sql.append("select ");
		if(count) sql.append("count(*) as num ");
		else sql.append("* ");
		sql.append("from "+tableName+" where ");
		String[] attrs = bean._getAttrNames();
		for (String attr : attrs) {
			sql.append(attr+"="+bean.getStr(attr));
			if(attr!=attrs[attrs.length-1]){
				sql.append(" and ");
			}
		}
		
		if(offset!=null && length!=null){
			sql.append(" limit "+offset+","+length);
		}
		
		sql.append(";");
		
		return sql.toString();
	}
	
}
